﻿
using Xacml.Core.Interfaces;
using Xacml.Core.Runtime;

namespace Xacml.Core.Runtime.Functions
{
  public abstract class BaseSubset : FunctionBase, ITypeSpecificFunction
  {
    public override IDataType Returns
    {
      get
      {
        return (IDataType) DataTypeDescriptor.Boolean;
      }
    }

    public override IDataType[] Arguments
    {
      get
      {
        return new IDataType[2]
        {
          (IDataType) DataTypeDescriptor.Bag,
          (IDataType) DataTypeDescriptor.Bag
        };
      }
    }

    public abstract IDataType DataType { get; }

    public override EvaluationValue Evaluate(EvaluationContext context, params IFunctionParameter[] args)
    {
      IFunction equalFunction = this.DataType.EqualFunction;
      foreach (object obj1 in args[0].Elements)
      {
        bool flag = false;
        foreach (object obj2 in args[1].Elements)
        {
          if (equalFunction.Evaluate(context, (IFunctionParameter) new EvaluationValue(obj1, args[0].GetType(context)), (IFunctionParameter) new EvaluationValue(obj2, args[1].GetType(context))).BoolValue)
          {
            flag = true;
            break;
          }
        }
        if (!flag)
          return EvaluationValue.False;
      }
      return EvaluationValue.True;
    }
  }
}
